
import React from "react";
import { ICapabilityHook } from "../../interfaces/ICapability";

export interface ICapabilityLoadProps {
    load: () => Promise<any>;
    render: (e: ICapabilityHook) => JSX.Element;
}

/** 能力加载器 */
export function CapabilityLoad(props: ICapabilityLoadProps) {

    const capabilityHook = React.useRef<any>();

    const [version, setVersion] = React.useState(0);

    const { load, render } = props;

    React.useEffect(() => {
        load().then(e => {
            capabilityHook.current = e;
            setVersion(e => e + 1);
        })
    }, [load]);

    if (version > 0) {
        return render(capabilityHook.current);
    } else {
        return <></>;
    }
}